package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限管理
 *
 * @author choujin
 * @create 2019/12/16 8:59
 */
@RequestMapping("/permission")
@RestController
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    /**
     * 权限条件的分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            return new Result(true, "权限查询成功", permissionService.findPage(queryPageBean));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "权限查询失败");
        }
    }

    /**
     * 权限新增功能
     *
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        Result result = checkPermission(permission);
        if (result != null) {
            return result;
        }
        //调用服务
        try {
            permissionService.add(permission);
            return new Result(true, "权限新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "权限新增失败");
        }
    }

    /**
     * 提取公共非空校验方法
     *
     * @param permission
     * @return
     */
    private Result checkPermission(@RequestBody Permission permission) {
        //去掉空格
        permission.setName(permission.getName().trim());
        permission.setDescription(permission.getDescription().trim());
        permission.setKeyword(permission.getKeyword().trim());
        System.out.println("==========="+permission.toString());
        //非空校验
        if (permission == null) {
            return new Result(false, "请正确提交数据");
        }
        //权限名非空校验
        if (permission.getName() == null || "".equals(permission.getName())) {
            return new Result(false, "权限名不能为空");
        }
        //权限编码
        if (permission.getKeyword() == null||"".equals(permission.getKeyword())) {
            return new Result(false, "权限编码不能为空");
        }
        return null;
    }

    /**
     * 编辑权限
     *
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        //id非空
        if (permission.getId() == 0 || permission.getId() == null) {
            return new Result(false, "id不能为空");
        }
        //其余项非空判断
        Result result = checkPermission(permission);
        if (result != null) {
            return result;
        }
        try {
            permissionService.edit(permission);
            return new Result(true, "权限编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "权限编辑失败");
        }
    }

    /**
     * 根据id查询权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        //非空校验
        if (id == 0) {
            return new Result(false, "请正确提交参数");
        }
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true, "权限查询成功", permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "权限查询失败");
        }
    }

    /**
     * 删除功能
     *
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public Result del(Integer id) {
        if (id == 0) {
            return new Result(false, "数据格式错误");
        }
        try {
            permissionService.del(id);
            return new Result(true, "权限删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "权限删除失败");
        }
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Permission> list = permissionService.findAll();
            return new Result(true,"权限查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限查询失败");
        }
    }

}
